{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRXccySwap, IRXccySwapFixFix\n",
    "from gs_quant.risk import Cashflows\n",
    "from gs_quant.session import Environment, GsSession\n",
    "\n",
    "# external users should substitute their client id and secret\n",
    "client_id = None  # Supply your application id\n",
    "client_secret = None  # Supply your client secret\n",
    "\n",
    "GsSession.use(Environment.PROD, client_id=client_id, client_secret=client_secret, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a Xccy float vs float Swap and a Xccy fix vs fix swap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": [
     "Instrument - XccySwap"
    ]
   },
   "outputs": [],
   "source": [
    "mtm_swap = IRXccySwap(payer_currency='EUR', receiver_currency='USD', effective_date='3m', termination_date='10y')\n",
    "fix_swap = IRXccySwapFixFix(\n",
    "    payer_currency='EUR', receiver_currency='USD', termination_date='10y', payer_rate=0.01, receiver_rate=0.015\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compute cashflows for the 10y EURUSD fix swap "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "Metrics - Cashflows"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>currency</th>\n",
       "      <th>payment_date</th>\n",
       "      <th>set_date</th>\n",
       "      <th>accrual_start_date</th>\n",
       "      <th>accrual_end_date</th>\n",
       "      <th>payment_amount</th>\n",
       "      <th>notional</th>\n",
       "      <th>payment_type</th>\n",
       "      <th>floating_rate_option</th>\n",
       "      <th>floating_rate_designated_maturity</th>\n",
       "      <th>day_count_fraction</th>\n",
       "      <th>spread</th>\n",
       "      <th>rate</th>\n",
       "      <th>discount_factor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2025-07-07</td>\n",
       "      <td>None</td>\n",
       "      <td>2025-04-02</td>\n",
       "      <td>2025-07-02</td>\n",
       "      <td>-250000.0</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>FIX</td>\n",
       "      <td>NA</td>\n",
       "      <td>NA</td>\n",
       "      <td>0.25</td>\n",
       "      <td>None</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.993976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2025-10-06</td>\n",
       "      <td>None</td>\n",
       "      <td>2025-07-02</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>-250000.0</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>FIX</td>\n",
       "      <td>NA</td>\n",
       "      <td>NA</td>\n",
       "      <td>0.25</td>\n",
       "      <td>None</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.989072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-01-06</td>\n",
       "      <td>None</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>-250000.0</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>FIX</td>\n",
       "      <td>NA</td>\n",
       "      <td>NA</td>\n",
       "      <td>0.25</td>\n",
       "      <td>None</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.984584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-04-08</td>\n",
       "      <td>None</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>-250000.0</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>FIX</td>\n",
       "      <td>NA</td>\n",
       "      <td>NA</td>\n",
       "      <td>0.25</td>\n",
       "      <td>None</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.980044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-07-06</td>\n",
       "      <td>None</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>-250000.0</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>FIX</td>\n",
       "      <td>NA</td>\n",
       "      <td>NA</td>\n",
       "      <td>0.25</td>\n",
       "      <td>None</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.975711</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  currency payment_date set_date accrual_start_date accrual_end_date  \\\n",
       "0      EUR   2025-07-07     None         2025-04-02       2025-07-02   \n",
       "1      EUR   2025-10-06     None         2025-07-02       2025-10-02   \n",
       "2      EUR   2026-01-06     None         2025-10-02       2026-01-02   \n",
       "3      EUR   2026-04-08     None         2026-01-02       2026-04-02   \n",
       "4      EUR   2026-07-06     None         2026-04-02       2026-07-02   \n",
       "\n",
       "   payment_amount     notional payment_type floating_rate_option  \\\n",
       "0       -250000.0  100000000.0          FIX                   NA   \n",
       "1       -250000.0  100000000.0          FIX                   NA   \n",
       "2       -250000.0  100000000.0          FIX                   NA   \n",
       "3       -250000.0  100000000.0          FIX                   NA   \n",
       "4       -250000.0  100000000.0          FIX                   NA   \n",
       "\n",
       "  floating_rate_designated_maturity  day_count_fraction spread  rate  \\\n",
       "0                                NA                0.25   None  0.01   \n",
       "1                                NA                0.25   None  0.01   \n",
       "2                                NA                0.25   None  0.01   \n",
       "3                                NA                0.25   None  0.01   \n",
       "4                                NA                0.25   None  0.01   \n",
       "\n",
       "   discount_factor  \n",
       "0         0.993976  \n",
       "1         0.989072  \n",
       "2         0.984584  \n",
       "3         0.980044  \n",
       "4         0.975711  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cf_fix = fix_swap.calc(Cashflows)\n",
    "cf_fix.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compute cashflows for the forward starting 10y EURUSD float float swap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>currency</th>\n",
       "      <th>payment_date</th>\n",
       "      <th>set_date</th>\n",
       "      <th>accrual_start_date</th>\n",
       "      <th>accrual_end_date</th>\n",
       "      <th>payment_amount</th>\n",
       "      <th>notional</th>\n",
       "      <th>payment_type</th>\n",
       "      <th>floating_rate_option</th>\n",
       "      <th>floating_rate_designated_maturity</th>\n",
       "      <th>day_count_fraction</th>\n",
       "      <th>spread</th>\n",
       "      <th>rate</th>\n",
       "      <th>discount_factor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2025-10-06</td>\n",
       "      <td>2025-07-02</td>\n",
       "      <td>2025-07-02</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>-501686.798657</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.255556</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.019835</td>\n",
       "      <td>0.989072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-01-06</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>-471412.499356</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.255556</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018651</td>\n",
       "      <td>0.984584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-04-08</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>-446566.760037</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018067</td>\n",
       "      <td>0.980044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-07-06</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>-451174.776800</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.252778</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018053</td>\n",
       "      <td>0.975711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-10-06</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>2026-10-02</td>\n",
       "      <td>-464690.352983</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.255556</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018387</td>\n",
       "      <td>0.971167</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  currency payment_date    set_date accrual_start_date accrual_end_date  \\\n",
       "0      EUR   2025-10-06  2025-07-02         2025-07-02       2025-10-02   \n",
       "1      EUR   2026-01-06  2025-10-02         2025-10-02       2026-01-02   \n",
       "2      EUR   2026-04-08  2026-01-02         2026-01-02       2026-04-02   \n",
       "3      EUR   2026-07-06  2026-04-02         2026-04-02       2026-07-02   \n",
       "4      EUR   2026-10-06  2026-07-02         2026-07-02       2026-10-02   \n",
       "\n",
       "   payment_amount     notional payment_type  floating_rate_option  \\\n",
       "0  -501686.798657  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "1  -471412.499356  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "2  -446566.760037  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "3  -451174.776800  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "4  -464690.352983  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "\n",
       "  floating_rate_designated_maturity  day_count_fraction    spread      rate  \\\n",
       "0                                3m            0.255556 -0.000204  0.019835   \n",
       "1                                3m            0.255556 -0.000204  0.018651   \n",
       "2                                3m            0.250000 -0.000204  0.018067   \n",
       "3                                3m            0.252778 -0.000204  0.018053   \n",
       "4                                3m            0.255556 -0.000204  0.018387   \n",
       "\n",
       "   discount_factor  \n",
       "0         0.989072  \n",
       "1         0.984584  \n",
       "2         0.980044  \n",
       "3         0.975711  \n",
       "4         0.971167  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mtm_swap.calc(Cashflows).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Clone the float float swap keeping spread constant but modifying the rate applied at initiation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": [
     "Instrument - Cloning"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>currency</th>\n",
       "      <th>payment_date</th>\n",
       "      <th>set_date</th>\n",
       "      <th>accrual_start_date</th>\n",
       "      <th>accrual_end_date</th>\n",
       "      <th>payment_amount</th>\n",
       "      <th>notional</th>\n",
       "      <th>payment_type</th>\n",
       "      <th>floating_rate_option</th>\n",
       "      <th>floating_rate_designated_maturity</th>\n",
       "      <th>day_count_fraction</th>\n",
       "      <th>spread</th>\n",
       "      <th>rate</th>\n",
       "      <th>discount_factor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2025-10-06</td>\n",
       "      <td>2025-07-02</td>\n",
       "      <td>2025-07-02</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>-501685.517234</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.255556</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.019835</td>\n",
       "      <td>0.989072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-01-06</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>2025-10-02</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>-471411.217933</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.255556</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018651</td>\n",
       "      <td>0.984584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-04-08</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>2026-01-02</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>-446565.506472</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018067</td>\n",
       "      <td>0.980044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-07-06</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>2026-04-02</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>-451173.509306</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.252778</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018053</td>\n",
       "      <td>0.975711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EUR</td>\n",
       "      <td>2026-10-06</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>2026-07-02</td>\n",
       "      <td>2026-10-02</td>\n",
       "      <td>-464689.071560</td>\n",
       "      <td>100000000.0</td>\n",
       "      <td>Flt</td>\n",
       "      <td>EUR-EuroSTR-COMPOUND</td>\n",
       "      <td>3m</td>\n",
       "      <td>0.255556</td>\n",
       "      <td>-0.000204</td>\n",
       "      <td>0.018387</td>\n",
       "      <td>0.971167</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  currency payment_date    set_date accrual_start_date accrual_end_date  \\\n",
       "0      EUR   2025-10-06  2025-07-02         2025-07-02       2025-10-02   \n",
       "1      EUR   2026-01-06  2025-10-02         2025-10-02       2026-01-02   \n",
       "2      EUR   2026-04-08  2026-01-02         2026-01-02       2026-04-02   \n",
       "3      EUR   2026-07-06  2026-04-02         2026-04-02       2026-07-02   \n",
       "4      EUR   2026-10-06  2026-07-02         2026-07-02       2026-10-02   \n",
       "\n",
       "   payment_amount     notional payment_type  floating_rate_option  \\\n",
       "0  -501685.517234  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "1  -471411.217933  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "2  -446565.506472  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "3  -451173.509306  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "4  -464689.071560  100000000.0          Flt  EUR-EuroSTR-COMPOUND   \n",
       "\n",
       "  floating_rate_designated_maturity  day_count_fraction    spread      rate  \\\n",
       "0                                3m            0.255556 -0.000204  0.019835   \n",
       "1                                3m            0.255556 -0.000204  0.018651   \n",
       "2                                3m            0.250000 -0.000204  0.018067   \n",
       "3                                3m            0.252778 -0.000204  0.018053   \n",
       "4                                3m            0.255556 -0.000204  0.018387   \n",
       "\n",
       "   discount_factor  \n",
       "0         0.989072  \n",
       "1         0.984584  \n",
       "2         0.980044  \n",
       "3         0.975711  \n",
       "4         0.971167  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mtm_swap = mtm_swap.clone(initial_fx_rate=1.2, payer_spread=mtm_swap.payer_spread)\n",
    "mtm_swap.calc(Cashflows).head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
